#include "YGConnection.h"

#include "Error.h"

namespace YoghurtGum
{

	Connection::Connection()
	{

	}

	Connection::~Connection()
	{

	}

	bool Connection::Connect(const char* a_Address, unsigned int a_Port)
	{
		m_Socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if (m_Socket == INVALID_SOCKET)
		{
			//ERROR_EXPLAIN("Couldn't create a new socket.");
			return false;
		}

		strcpy(m_Address, a_Address);
		m_Port = a_Port;

		m_Instance.sin_family       = AF_INET;
		m_Instance.sin_addr.s_addr  = inet_addr(m_Address);
		m_Instance.sin_port         = htons(m_Port);	

		if (::connect(m_Socket, (SOCKADDR*)&m_Instance, sizeof(sockaddr_in)) == SOCKET_ERROR)
		{
			char error[256];
			sprintf(error, "Couldn't connect to '%s' on port %d.", m_Address, m_Port);
			//ERROR_EXPLAIN(error);
			return false;
		}

		return true;
	}

}; // namespace YoghurtGum